﻿layui.define(['index', 'jquery', 'form', 'laypage','layer'], function (exports) {
    var index = layui.index,
        form = layui.form,
        laypage = layui.laypage,
        layer=layui.layer,
        $ = layui.$;

    var pageName = layui.cache.module.pageName;
    var $body = $('body');
    var module = {
        //初始化编辑器
        initEditor: function (relationGuid) {
            index.layEditor({
                elem: '.fly-editor',
                relationGuid: relationGuid,
                fileTage: $('.fly-editor').attr('file-tag')
            });
        },
        //分页处理
        replyInit: function (postId) {
            laypage.render({
                elem: 'replyPage',
                count: layui.cache.module.totalCount,
                limit: 10,
                curr: layui.cache.module.pageIndex,
                jump: function (obj, first) {
                    window.location.href = '/Post/Detail?postId=' + postId + '&page=' + obj.curr;
                }
            });
        }
    }

    //事件集合
    var events = {
        //帖子回复采纳
        accept: function (that) {
            var postId = layui.cache.module.postId;
            var replyId = $(that).attr('replyId');

            layer.confirm('确认采纳此评论吗？', function (res) {
                layer.close(res);
                index.jsonAjax('/PostReply/PostReplyAcceptAsync', {
                    postId: postId,
                    replyId: replyId
                }, function () {
                    module.replyInit(postId);
                });
            });
        },

        //帖子评论删除
        delete: function (that) {
            var postId = layui.cache.module.postId;
            var replyId = $(that).attr('replyId');

            layer.confirm('确认删除此评论吗？', function (res) {
                layer.close(res);
                index.jsonAjax('/PostReply/ReplyDeleteAsync', {
                    postId: postId,
                    replyId: replyId
                }, function () {
                    module.replyInit(postId);
                });
            });
        }
    }

    //发布页
    if (pageName === 'publishPage') {
        var relationGuid = $('#relationGuid').val();

        module.initEditor(relationGuid);

        //监听表单的提交事件，将内容转成Html，并提交
        form.on('submit(postPublish)',
            function (result) {
                result.field.HtmlContent = index.content(result.field.Content);
                debugger
                index.jsonAjax('/Post/Publish',
                
                    {
                        saveEntity: result.field
                    },
                    function (data) {
                        window.location.href = '/Post/Detail?postId=' + data;
                    });
            });
    }

    if (pageName === 'detailPage') {
       
        var postId = layui.cache.module.postId;

        //初始化编辑器
        var relationGuid = $('#relationGuid').val();
        module.initEditor(relationGuid);

        //初始化分页
        laypage.render({
            elem: 'replyPage',//这里不放#
            count: layui.cache.module.totalCount,
            limit: 10,
            curr: layui.cache.module.pageIndex,
            jump: function (obj, first) {
                if (!first) {
                    window.location.href = '/Post/Detail?postId=' + postId + '&page=' + obj.curr;
                }
            }
        });
        //帖子操作
        $('#post_Operate').find('*[operate-type]').click(function () {
            var that = this;
            var operateType = $(that).attr('operate-type');

            index.jsonAjax('/Post/PostOperate',
                {
                    postId: postId,
                    operate: operateType
                },
                function () {
                    //如果被删除了，就直接跳转至主页
                    if (operateType === '10') {
                        window.location.href = '/';
                    } else {
                        //直接刷新当前页
                        window.location.reload();
                    }
                },
                {
                    error: function () {
                        window.location.href = '/';
                    }
                });
        });

        //帖子收藏/取消收藏
        $('#postCollection').find('*[post-collection]').click(function () {
            var that = this;
            var status = $(that).attr('post-collection');
            if (status === 'collection') {
                index.jsonAjax('/Post/PostCollection', {
                    postId: postId
                }, function () {
                    window.location.reload();
                });
            }
            if (status === 'cancel') {
                index.jsonAjax('/Post/CollectionCancel', {
                    postId: postId
                }, function () {
                    window.location.reload();
                });
            }
        });

        //评论提交监听
        form.on('submit(postReply)', function (result) {
            result.field.ReplyContentHtml = index.content(result.field.ReplyContent);
            index.jsonAjax('/PostReply/PostReplyAsync', {
                saveEntity: result.field
            }, function () {
                layer.msg('评论已提交!', { time: 5000 });
                laypage.render({
                    elem: 'replyPage',
                    count: layui.cache.module.totalCount,
                    limit: 10,
                    curr: layui.cache.module.pageIndex,
                    jump: function (obj, first) {
                        window.location.href = '/Post/Detail?postId=' + postId + '&page=' + obj.curr;
                    }
                });
            });
        });
    }

    if (pageName === 'listPage') {
        var pageIndex = layui.cache.module.pageIndex,
            totalCount = layui.cache.module.totalCount,
            postType = layui.cache.module.postType,
            postStatus = layui.cache.module.postStatus,
            postSort = layui.cache.module.postSort;

        laypage.render({
            elem: 'postPage',
            count: totalCount,
            limit: 20,
            curr: pageIndex,
            jump: function (obj, first) {
                if (!first) {
                    window.location.href = '/Home/Index?pageIndex=' +
                        obj.curr +
                        '&postType=' +
                        postType +
                        '&postStatus=' +
                        postStatus +
                        '&postSort=' +
                        postSort
                }
            }
        });
    }

    //注册全局事件
    $body.on('click', '*[click-event]', function () {
        var that = $(this);
        //获取自定义的事件名称
        var eventAttr = that.attr('click-event');
        //执行事件
        events[eventAttr] && events[eventAttr](this);
    });

    exports('post', null);
})